﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace calendar
{
public sealed class  DateUtility
{
    public static DateTime getDayStartDate(DateTime date)
    {
        if (date != null) {
            return new DateTime(date.Year, date.Month, date.Day, 0, 0, 0, 0);
        } else {
            return date;
        }
    }

    public static DateTime getDayEndDate(DateTime date)
    {
        if (date != null) {
            return new DateTime(date.Year, date.Month, date.Day, 23, 59, 59, 999);
        } else {
            return date;
        }
    }

    public static DateTime getCanonicalTime(DateTime date)
    {
        if (date != null) {
            return new DateTime(1, 1, 1, date.Hour, date.Minute, date.Second, 0);
        } else {
            return date;
        }
    }

    public static int compare(DateTime startDate, DateTime endDate, DateTime targetDate)
    {
        return compare(startDate, endDate, targetDate.Ticks);
    }

    public static int compare(DateTime startDate, DateTime endDate, long p)
    {
        int result = 0;
        if (p < startDate.Ticks) {
            result = -1;
        } else {
            if (p > endDate.Ticks) {
                result = 1;
            }
        }
        return result;
    }

    public static int compare(DateTime d1, DateTime d2)
    {
        int result = 0;
        if (d1 == null || d2 == null) {
            result = (d1 == d2 ? 0 : (d1 == null ? 1 : -1));
        } else {
            long diff = d1.Ticks - d2.Ticks;
            result = ((diff == 0) ? 0 : ((diff > 0) ? 1 : -1));
        }
        return result;
    }

    public static DateTime getDateFromLong(long date)
    {
        return (new DateTime(date));
    }

    public static DateTime getTime(int hour, int minutes)
    {
        DateTime tmp = DateTime.Now;
        return new DateTime(tmp.Year, tmp.Month, tmp.Day, hour, minutes, 0);

    }

    public static DateTime setTime(DateTime date, DateTime canonicalTime)
    {
        DateTime result;
        if (canonicalTime == null) {
            result = date;
        } else {
            result = DateUtility.getDayStartDate(date);
            long offset = canonicalTime.Ticks - CANONICAL_EPOCH.Ticks;
            result = new DateTime(result.Ticks + offset);
        }

        return result;
    }

    private static DateTime CANONICAL_EPOCH = getCanonicalTime(getDayStartDate(new DateTime()));
    
}
}
